/**
 * Copyright c FOUNDER CORPORATION 2006 All Rights Reserved.
 * DbFunctionUtil.java
 */
package com.iori.common.util;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
 *
 * [クラス名]<br>
 * DbFunctionUtilクラス。<br>
 * <br>
 * [機能概要]<br>
 * 读取xml配置文件路径的工具类<br>
 * <br>
 * [変更履歴]<br>
 * 2007/12/20 新規作成 WHB<br>
 *
 * @author WHB
 * @version 1.00
 */
public class DbFunctionUtil {
	/**
	 * DbFunctionUtilClass用配置文件的路径
	 */
	private static final String DB_FUNCTION_FILE_NAME = "dbFunction.xml";
	private static String InstanceName;
	private DbFunctionUtil(){
	}
	/**
	 * 读取dbFunction.xml中记述的tag値(InstanceName)。<br>
	 * 单实例
	 * @return String
	 *
	 *
	 */
	public static String getInstanceName(){
		if(InstanceName==null){
		try {
//		String xmlPath = "dbFunction.xml";
		String xmlPath = XmlConfigPath.getXmlConfigPath() + DB_FUNCTION_FILE_NAME;
		InputStream is=Thread.currentThread().getContextClassLoader().getResourceAsStream(xmlPath);
		InstanceName = parse(is);
	} catch (Exception e) {
		e.printStackTrace();	}
		}
	   return InstanceName;
	}
	/**
	 *  解析xml文件，读取dbFunction.xml中记述的tag値(InstanceName)。<br>
	 *
	 * @return String
	 *
	 *
	 */
	private static String parse(InputStream xmlStream) throws Exception {
	    String nodeValue;
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

		DocumentBuilder db = null;
		try {
			db = dbf.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			throw new Exception (" DbFunctionUtil.parse to deploy "+e.getMessage());
		}

		Document doc = null;
		try {
			doc = db.parse(xmlStream);
		} catch (SAXException e) {
			throw new Exception (" DbFunctionUtil.parse's sax to analyze "+e.getMessage());
		} catch (IOException e) {
			throw new Exception (" DbFunctionUtil.parse's io exception "+e.getMessage());
		}

		NodeList useCaseNodeList = doc.getElementsByTagName("InstanceName");

			Node rootNode = useCaseNodeList.item(0);
			try {
					 Node node= rootNode.getFirstChild();
					 nodeValue=node.getNodeValue();
			} catch (Exception  e) {
				throw new Exception (" DbFunctionUtil.parse's exception " + e.getMessage());
			}

			return nodeValue;
	}
}
